#ifndef __LINK_SOCKET_H_
#define __LINK_SOCKET_H_

#include <pthread.h>
#include <sys/time.h>
#include "libprotocol.h"

#ifndef PORT_HOST
  #define PORT_HOST 8016
#endif

/*
 * @brief socket parameter struct
 */
#define SOCKET          int
typedef struct {
    union {
        SOCKET socket;
    }u;
#define sen_socket  u.socket
    int ret;
    int chainId;
}SenLink;

typedef struct linkDes{
    int         linkFd;           // fd of client 
    SenLink     link;             // type for read & write 
    int         linkId;           // link index
    ProChan     chan;             // protocol channel
    char        ipAddr[15];       // client ip address
    int         port;             // clinet port
    void        *proParseExe;     // operation execute
    pthread_t   commPth;          // communication pthread
}linkDes_t;

typedef struct linkList{
    linkDes_t   link;
    struct      linkList *next;
}linkList_t;

typedef enum listOp{
    CREAT_LIST,
    SELECT_LIST,
    INSERT_LIST,
    DELET_LIST,
    TRAVE_LIST,
}typeListOp;

typedef enum linkSigType{
    noLinkSig   = 0,
    addLinkSig,
    deletLinkSig,
}linkSigType_t;

typedef struct linkSigInfo{
    linkDes_t     client;
    linkSigType_t linkSigType;
}linkSigInfo_t;

typedef enum linkCtlType{
    NO_LINK_CTL = 0,
    ADD_LINK_CTL,
    DEL_LINK_CTL,
}linkCtlType_t;

typedef struct linkCtlInfo{
    linkDes_t     server;
    linkCtlType_t linkCtlType;
}linkCtlInfo_t;

extern linkList_t      *gClientListHead;
extern linkList_t       *gServerLinkHead;

int sizeOfLinkList(linkList_t *head);
linkList_t *findLinkNodeFromList(linkList_t *head, int linkId);

int tcpServerInit(void *fun_pro_parse_exe);

int tcpDevConnectCtl(linkCtlInfo_t linkCtlInfo, void *proParseExe);

#endif /*__LINK_SOCKET_H__*/
